package com.xue.apitest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jayway.jsonpath.JsonPath;
import com.xue.apitest.constant.API;
import com.xue.apitest.constant.ResultEntity;
import com.xue.apitest.entity.User;
import com.xue.apitest.mapper.UserMapper;
import com.xue.apitest.utils.ExcelUtils;
import com.xue.apitest.utils.JsonUtils;
import com.xue.apitest.utils.RestTemplateUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.web.client.RestTemplate;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.*;

@SpringBootTest
@MapperScan("com.xue")
public class Demo extends AbstractTestNGSpringContextTests {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RestTemplate restTemplate;
    String baseURL = "http://test.elearning.learnfuture.com";
    HttpHeaders headers = new HttpHeaders();

    @Test
    void contextLoads() {

        String url = baseURL+"/adminapi/api/user/v1.0/listByDataPermission/?pageSize=10&pageNum=1&departmentId=6&cateId=6&bankCategory=6";
        headers.add("menuId", "8");
        headers.add("Authorization", "Bearer 53f90f17-8858-43d5-a253-346a22516dc7 ");
        HttpEntity<Object> entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        System.out.println(response);
    }

    @Test
    public void test1(){
        String URI = baseURL+"/adminapi/api/user/v1.0/post/";
        String data = "{\"passport\":\"aaaaa11111aaaaaaaaaa311111aaaaa\",\"name\":\"aa11accc\",\"sex\":\"\",\"birthday\":\"\",\"departmentId\":6,\"positionId\":\"\",\"rankId\":\"\",\"managerId\":\"\",\"mobile\":\"\",\"entryDate\":\"\",\"sn\":\"\",\"email\":\"\",\"expirationDate\":\"\"}";
        Map<String, String> map = new HashMap<>();
        map.put("token","Bearer 53f90f17-8858-43d5-a253-346a22516dc7 ");
        map.put("menuId","8");
        ResponseEntity<String> response = RestTemplateUtils.postForJson(URI, map, data, String.class);
        System.out.println(response);
        ResultEntity<?> o = JsonUtils.jsonToObj(response.getBody());
        System.out.println(Objects.requireNonNull(o).getData());
        Assert.assertEquals(Objects.requireNonNull(o).getCode(),1);
    }

    @Test
    public void test2(){
        List<API> apis = ExcelUtils.readExcel(0, API.class);
        assert apis != null;
        for (API api : apis) {
            System.out.println(api);
        }
    }

    @Test
    public void test3(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        map.put("password","123456");
        map.put("name", "张三");
        wrapper.allEq(map);
        System.out.println(userMapper.selectOne(wrapper));
    }

    @Test
    public void test4(){
        System.out.println("========================================");
        System.out.println(userMapper.selectUserByPassport("123456"));
        User user = userMapper.selectUserByPassport("123456");
        System.out.println("name====================="+user.getName());

    }

    @Test
    public void test5()throws Exception{
        String json = "{\n" +
                "    \"store\": {\n" +
                "        \"book\": [\n" +
                "            {\n" +
                "                \"category\": \"reference\",\n" +
                "                \"author\": \"Nigel Rees\",\n" +
                "                \"title\": \"Sayings of the Century\",\n" +
                "                \"price\": 8.95\n" +
                "            },\n" +
                "            {\n" +
                "                \"category\": \"fiction\",\n" +
                "                \"author\": \"Evelyn Waugh\",\n" +
                "                \"title\": \"Sword of Honour\",\n" +
                "                \"price\": 12.99\n" +
                "            },\n" +
                "            {\n" +
                "                \"category\": \"fiction\",\n" +
                "                \"author\": \"Herman Melville\",\n" +
                "                \"title\": \"Moby Dick\",\n" +
                "                \"isbn\": \"0-553-21311-3\",\n" +
                "                \"price\": 8.99\n" +
                "            },\n" +
                "            {\n" +
                "                \"category\": \"fiction\",\n" +
                "                \"author\": \"J. R. R. Tolkien\",\n" +
                "                \"title\": \"The Lord of the Rings\",\n" +
                "                \"isbn\": \"0-395-19395-8\",\n" +
                "                \"price\": 22.99\n" +
                "            }\n" +
                "        ],\n" +
                "        \"bicycle\": {\n" +
                "            \"color\": \"red\",\n" +
                "            \"price\": 19.95\n" +
                "        }\n" +
                "    },\n" +
                "    \"expensive\": 10\n" +
                "}";
        Object  read = JsonPath.read(json, "$.store.book..category");
        System.out.println("read..........."+read);



    }

    @Test
    void test11(){
        System.out.println(2222);
    }




}
